





{include file='client/main.tpl'}







<main class="content">
        <div class="col-lg-12 col-sm-12">
            <section class="content-inner margin-top-no">

                <div class="card">
                    <div class="card-main">
                        <div class="card-inner">
                            <p>系统中所有商品的列表。您购买等级类的商品时有效期会从当前时间开始计算。</p>
                            <p>当前余额：{$user->money} 元</p>
                        </div>
                    </div>
                </div>

                <div class="table-responsive">
                    {$shops->render()}
                    <table class="table ">
                        <tr>
                            <th>操作</th>
                            <th>ID</th>
                            <th>名称</th>
                            <th>价格</th>
                            <th>内容</th>
                            <th>自动续费天数</th>
                            <th>续费时重置流量</th>

                        </tr>
                        {foreach $shops as $shop}
                            <tr>
                                <td>
                                    <a class="btn btn-brand-accent" href="javascript:void(0);" onClick="buy('{$shop->id}',{$shop->auto_renew},{$shop->auto_reset_bandwidth})">购买</a>
                                </td>
                                <td>#{$shop->id}</td>
                                <td>{$shop->name}</td>
                                <td>{$shop->price} 元</td>
                                <td>{$shop->content()}</td>
                                {if $shop->auto_renew==0}
                                    <td>不能自动续费</td>
                                {else}
                                    <td>可选 在 {$shop->auto_renew} 天后自动续费</td>
                                {/if}

                                {if $shop->auto_reset_bandwidth==0}
                                    <td>不自动重置</td>
                                {else}
                                    <td>自动重置</td>
                                {/if}

                            </tr>
                        {/foreach}
                    </table>
                    {$shops->render()}
                </div>


                <div aria-hidden="true" class="modal modal-va-middle fade" id="coupon_modal" role="dialog" tabindex="-1">
                    <div class="modal-dialog modal-xs">
                        <div class="modal-content">
                            <div class="modal-heading">
                                <a class="modal-close" data-dismiss="modal">×</a>
                                <h2 class="modal-title">您有优惠码吗？</h2>
                            </div>
                            <div class="modal-inner">
                                <div class="form-group form-group-label">
                                    <label class="floating-label" for="coupon">有的话，请在这里输入。没有的话，直接确定吧</label>
                                    <input class="form-control" id="coupon" type="text">
                                </div>
                            </div>
                            <div class="modal-footer">
                                <p class="text-right"><button class="btn btn-flat btn-brand waves-attach" data-dismiss="modal" id="coupon_input" type="button">确定</button></p>
                            </div>
                        </div>
                    </div>
                </div>


                <div aria-hidden="true" class="modal modal-va-middle fade" id="order_modal" role="dialog" tabindex="-1">
                    <div class="modal-dialog modal-xs">
                        <div class="modal-content">
                            <div class="modal-heading">
                                <a class="modal-close" data-dismiss="modal">×</a>
                                <h2 class="modal-title">订单确认</h2>
                            </div>
                            <div class="modal-inner">
                                <p id="name">商品名称：</p>
                                <p id="credit">优惠额度：</p>
                                <p id="total">总金额：</p>
                                <p id="auto_reset">在到期时自动续费</p>

                                <div class="checkbox switch" id="autor">
                                    <label for="autorenew">
                                        <input checked class="access-hide" id="autorenew" type="checkbox"><span class="switch-toggle"></span>自动续费
                                    </label>
                                </div>

                            </div>

                            <div class="modal-footer">
                                <p class="text-right"><button class="btn btn-flat btn-brand waves-attach" data-dismiss="modal" id="order_input" type="button">确定</button></p>
                            </div>
                        </div>
                    </div>
                </div>

                {include file='dialog.tpl'}

        </div>



</main>
{include file='client/footer.tpl'}


<script>
    function buy(id,auto,auto_reset) {
        auto_renew=auto;
        if(auto==0)
        {
            document.getElementById('autor').style.display="none";
        }
        else
        {
            document.getElementById('autor').style.display="";
        }

        if(auto_reset==0)
        {
            document.getElementById('auto_reset').style.display="none";
        }
        else
        {
            document.getElementById('auto_reset').style.display="";
        }

        shop=id;
        $("#coupon_modal").modal();
    }


    $("#coupon_input").click(function () {
        $.ajax({
            type: "POST",
            url: "/user/coupon_check",
            dataType: "json",
            data: {
                coupon: $("#coupon").val(),
                shop: shop
            },
            success: function (data) {
                if (data.ret) {
                    $("#name").html("商品名称："+data.name);
                    $("#credit").html("优惠额度："+data.credit);
                    $("#total").html("总金额："+data.total);
                    $("#order_modal").modal();
                } else {
                    $("#result").modal();
                    $("#msg").html(data.msg);
                }
            },
            error: function (jqXHR) {
                $("#result").modal();
                $("#msg").html(data.msg+"  发生了错误。");
            }
        })
    });

    $("#order_input").click(function () {

        if(document.getElementById('autorenew').checked)
        {
            var autorenew=1;
        }
        else
        {
            var autorenew=0;
        }

        $.ajax({
            type: "POST",
            url: "/user/buy",
            dataType: "json",
            data: {
                coupon: $("#coupon").val(),
                shop: shop,
                autorenew: autorenew
            },
            success: function (data) {
                if (data.ret) {
                    $("#result").modal();
                    $("#msg").html(data.msg);
                    window.setTimeout("location.href='/client/shop'", {$config['jump_delay']});
                } else {
                    $("#result").modal();
                    $("#msg").html(data.msg);
                }
            },
            error: function (jqXHR) {
                $("#result").modal();
                $("#msg").html(data.msg+"  发生了错误。");
            }
        })
    });

</script>